<?php
namespace app\common\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Log;
use app\common\request\Curl;
use app\common\model\CarInfo;
use app\common\request\ClientLai;
use app\common\model\CarsInfo as CarsInfoModel;
class CarsInfo extends Command
{
    protected function configure()
    {
        $this->setName('cars')->setDescription('车辆数据获取');
    }

    protected function execute(Input $input, Output $output)
    {
        $sTime = microtime(true);
        // if(self::diffCars()){
        //     self::getCarInfo();
        //     $eTime = microtime(true);
        // }
        self::testNullIot();
        $eTime = microtime(true);
        $time = $eTime - $sTime;
        Log::record('[ getCarInfo ]'.$time,'log');
    	$output->writeln('消耗时间'.$time.'秒');
    }

    protected static function testNullIot()
    {
        Db::name('cars_info')->where('supplier_no',1376920003)->field('car_unique_id')->chunk(100,function($cars){
            $data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
            $cars = implode(',',$data['car_unique_id_arr']);
            $config = config('local_iot');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/car/getRtInfo',http_build_query(['car_unique_id'=>$cars]));
            $arr = json_decode($res,true);
            if($arr['result'] == 0){
                try {
                    $model = new CarsInfoModel;
                    $number = $model->allowField(true)->saveAll(array_filter($arr['data']));
                    
                } catch (\Exception $e) {
                    Log::record($e->getMessage(),'error');
                    var_dump($e->getMessage());
                    return false;
                }
                var_dump(count($number));
                return true;
            }   
        });
        
    }

    public static function diffCars()
    {   
        $cars = Db::name('cars')->where([
                            'supplier_no' => ['NEQ',1376920002],
                            'delete_time' => null
                        ])->where('car_sn','not null')
                        ->count();
        $info = CarInfo::count();
        if($cars==$info){
            return true;
        }else{
            $cars_data = Db::name('cars')
                            ->where('supplier_no','NEQ',1376920002)
                            ->where('delete_time',null)
                            ->field('car_unique_id,car_sn license_plate')
                            ->select();
            $model = new CarInfo();
            $model->execute('TRUNCATE `joy_car_info`');
            return $model->insertAll($cars_data);
        }
    }

    public static function getCarInfo()
    {
        CarInfo::field('car_unique_id')->chunk(100,function($cars){
            $curl = new Curl; 
            $header = config('car_hear.header');
            $url = config('car_hear.url');
            $data['car_unique_id_arr'] = array_column($cars,'car_unique_id');
            $res = $curl->set('CURLOPT_HTTPHEADER',$header)->post($data)->url($url);
             // 任务结果状态
            if ($curl->error()) {
                Log::record($curl->message(),'error');exit();
            } else {
                // 任务进程信息
                $info = $curl->info();               
                if($info['http_code'] == 500){ 
                    Log::record('对方服务器错误','error');  
                    exit();
                }else{
                    // 任务结果内容
                    $content = $curl->data();
                }
            }
            $arr = json_decode($content,true);
            if(!empty($arr['data'])){
                try {
                    $model = new CarInfo;
                    $array = array_values($arr['data']);
                    $model->allowField(true)->saveAll($array);
                } catch (\Exception $e) {
                    Log::record($e->getMessage(),'error');
                    return false;
                }
                return true;
            }
        }); 
    }
}